package utils

import (
	"fmt"
)

type TWTaskRec struct {
	handle uint32
	id     interface{}
	n      int64
	cb     interface{}

	aftercall func()
	info_idx  int64
	breakflag int
}

func (this *TWTaskRec) DoTask() {
	if GoFunCatchException {
		defer PanicHandler()
	}
	if this.breakflag == 1 {
		return
	}
	if this.cb != nil {
		if fn, ok := this.cb.(func()); ok {
			fn()
		} else if fn, ok := this.cb.(func() bool); ok {
			if !fn() {
				this.breakflag = 1
			}
		}
	}
	// 不能在这里执行, doTask可能会异步执行
	// this.doAfterCall();
}

/***
 * 必须要在命令同步通道中运行运行
 */
func (this *TWTaskRec) doAfterCall() {
	if this.aftercall != nil {
		this.aftercall()
	}
}

func (this *TWTaskRec) String() string {
	return fmt.Sprintf("%v", this.id)
}
